<template>
	<view>
		<u-tabs :list="tabs" @click="changeTab" activeColor="#0F0F0F" keyName="name" lineColor="#FFCDB8"
			lineHeight="5rpx" :scroll="true"></u-tabs>
		<!-- 	<v-tabs activeColor="#0F0F0F" lineColor="#FFCDB8" lineHeight="5rpx" lineScale="0.5" :scroll="true"
			v-model="current" :tabs="tabs" @change="changeTab"></v-tabs> -->
		<!-- <view class="select flex align between">
			<view :class="hotS?'selClass':''" @click="toSelHot">热度</view>
			<view :class="timeS?'selClass':''" @click="toSelTime">按时间</view>
			<view style="width: 70%;">
				<uni-search-bar v-model="searchValue" @input="input" placeholder="输入要查询的文案">
				</uni-search-bar>
			</view>
		</view> -->
		<view class="select flex align between">
			<view style="width: 80%;">
				<uni-search-bar v-model="searchValue" @input="input" placeholder="输入要查询的文案标题">
				</uni-search-bar>
			</view>
			<view class="addBtn flex align" @click="show = true">添加文案<uni-icons type="plus" size="20"
					color="#fff"></uni-icons></view>
		</view>

		<view class="list flex align between" v-for="(item,index) in list" :key="index">
			<view url="/pages/intellect/info" hover-class="none" class="list-info">
				<view>{{item.title}}</view>
				<view>{{item.content}}</view>
				<view>{{item.addtime}}</view>
			</view>
			<view class="btn" @click="toCopy(item.content)">复制文案</view>
		</view>
		<uni-load-more :status="status" v-if="!empty" :content-text="loadText"></uni-load-more>
		<xw-empty :isShow="empty" text="暂无数据" textColor="#777777"></xw-empty>
		<u-popup :show="show" :round="10" mode="bottom">
			<view class="btomShow">
				<view class="closeempty" @click="show = false">
					<uni-icons type="closeempty" size="30"></uni-icons>
				</view>
				<view class="showTit">添加素材</view>
				<view class="title">分类</view>
				<input placeholder="请选择" @click="show2 = true" disabled v-model="category_id_text" />
				<view class="title">标题</view>
				<input placeholder="请输入标题" v-model="title" />
				<view class="title">内容</view>
				<textarea placeholder="请输入内容" v-model="content"></textarea>
				<view class="subBtn" @click="toAdd">确定</view>
			</view>
		</u-popup>
		<u-picker :show="show2" :columns="columns" keyName="name" @cancel="show2 = false" @confirm="onConfirm"></u-picker>
	</view>
</template>

<script>
	import {
		articleCategory,
		articleList,
		articleAdd
	} from '@/api/apiData.js';
	export default {
		data() {
			return {
				category_id_text: '',
				category_ids: '',
				title: '',
				content: '',
				current: 0,
				tabs: [{
					name: '全部',
					id: ''
				}],
				searchValue: '',
				hotS: false,
				timeS: false,
				category_id: '',
				totalPage: 1,
				page: 0,
				offset: 20,
				list: [],
				scrollTop: 0,
				empty: false,
				loadText: {
					loadmore: '点击或上拉加载更多',
					loading: '努力加载中',
					nomore: '实在没有了'
				},
				status: 'loadmore',
				show: false,
				show2: false,
				columns:[]
			};
		},
		onLoad() {
			this.method_name()
			this.getlist();
		},
		onPageScroll(e) {
			const that = this;
			that.scrollTop = e.scrollTop;
		},
		onReachBottom() {
			const that = this;
			that.getlist();
		},
		methods: {
			async toAdd(){
				if(this.category_ids==''){
					uni.showToast({
						title:'请选择分类',
						icon:'error'
					})
					return false
				}
				let obj = {
					'row[category_id]':this.category_ids,
					'row[title]':this.title,
					'row[content]':this.content,
					'row[status]':1
				}
				const result = await articleAdd(obj)
				if(result.code==1){
					uni.showToast({
						title:'添加成功'
					})
					this.show = false
					this.page = 0
					this.list = []
					this.getlist()
				}else{
					uni.showToast({
						title:result.msg,
						icon:'error'
					})
				}
			},
			onConfirm(e){
				this.category_id_text = e.value[0].name
				this.category_ids = e.value[0].id
				this.show2 = false
			},
			toCopy(text) {
				// const extractedText = this.getTextFromHTML(text);
				// console.log(extractedText)
				uni.setClipboardData({
					data: text,
					success: function() {
						// 可以添加用户友好的提示，例如使用uni.showToast提示复制成功
						uni.showToast({
							title: '复制成功',
							icon: 'success',
							duration: 2000
						});
					}
				});
			},
			getTextFromHTML(val) {
				if (val != null && val != '') {
					let reg = /[\u4e00-\u9fa5]/g;
					let names = val.match(reg);
					val = names ? names.join('') : '';
					return val;
				} else return '';
			},
			async getlist() {
				const that = this;
				const {
					totalPage,
					page,
					offset,
					list,
					category_id,
					searchValue
				} = that;
				let obj = {
					menu_url: 'article',
					limit: offset,
					offset: page,
					sort: 'id',
					order: 'desc',
					_: new Date().getTime(),
					filter: {},
					op: {}
				};
				if (category_id != '') {
					if (searchValue != '') {
						obj.filter = JSON.stringify({
							'category_id': category_id,
							'title': searchValue
						})
						obj.op = JSON.stringify({
							'category_id': 'FIND_IN_SET',
							'title': '='
						})
					} else {
						obj.filter = JSON.stringify({
							'category_id': category_id
						})
						obj.op = JSON.stringify({
							'category_id': 'FIND_IN_SET'
						})
					}
				} else {
					if (searchValue != '') {
						obj.filter = JSON.stringify({
							'title': searchValue
						})
						obj.op = JSON.stringify({
							'title': '='
						})
					} else {
						obj.filter = {}
						obj.op = {}
					}
				}
				that.status = 'loading';
				if (totalPage > page) {
					let result = await articleList(obj);
					that.page = page + 1;
					if (result.total == 0) {
						that.empty = true;
						that.status = 'nomore';
						return;
					}
					that.empty = false;
					setTimeout(() => {
						that.status = 'loadmore';
					}, 500);
					that.list = list.concat(result.rows);
					that.totalPage = Math.ceil(result.total / offset);
				} else {
					setTimeout(() => {
						that.status = 'nomore';
					}, 500);
				}
			},
			async method_name() {
				const result = await articleCategory()
				this.columns = [result]
				this.tabs = this.tabs.concat(result)
			},
			changeTab(index) {
				this.category_id = index.id
				this.page = 0
				this.list = []
				this.getlist()
				// console.log('当前选中的项：' + index)
				// this.current = index
			},
			input(e) {
				console.log(e)
				this.page = 0
				this.list = []
				this.getlist()
			},
			toSelHot() {
				this.hotS = !this.hotS
			},
			toSelTime() {
				this.timeS = !this.timeS
			}
		}
	}
</script>

<style lang="scss" scoped>
	.btomShow {
		padding: 0 28rpx 50rpx;
		position: relative;

		.showTit {
			text-align: center;
			font-size: 32rpx;
			font-weight: bold;
			padding: 33rpx 0;
		}

		input,
		textarea {
			font-size: 20rpx;
			padding: 16rpx 24rpx;
			border-radius: 10rpx;
			border: solid 1rpx #707070;
		}

		textarea {
			max-height: 200rpx;
		}


		.closeempty {
			position: absolute;
			top: 20rpx;
			right: 30rpx;

		}

		.title {
			font-size: 28rpx;
			margin: 25rpx 0;
		}

		.subBtn {
			font-size: 30rpx;
			color: white;
			padding: 8rpx 95rpx;
			background: #5D9CEB;
			width: fit-content;
			margin: 50rpx auto 0rpx;
			border-radius: 10rpx;
		}
	}

	.select {
		background: white;
		padding: 0rpx 10rpx 0 0;

		.addBtn {
			background: #FFCDB8;
			color: white;
			border-radius: 14rpx;
			font-size: 20rpx;
			padding: 8rpx 14rpx;
		}
	}

	.selClass {
		color: #FFCDB8;
		font-weight: bold;
	}

	.list {
		background: white;
		padding: 17rpx 26rpx;
		margin-top: 2rpx;

		.list-info {
			width: 80%;

			view:nth-child(1) {
				font-size: 24rpx;
				margin-bottom: 11rpx;
			}

			view:nth-child(2),
			view:nth-child(3) {
				font-size: 19rpx;
				color: #8A8A8A;
				margin-bottom: 10rpx;
			}
		}

		.btn {
			background: #FFCDB8;
			font-size: 24rpx;
			border-radius: 8rpx;
			padding: 17rpx 19rpx;
		}
	}
</style>